<?php


namespace App\Http\Controllers\Weapp;


use App\Tool\Json;

class ShopCar extends Auth
//class ShopCar extends Base
{


    /**
     * 购物车添加商品
     *
     * 接口请求参数
     * id 商品规格id
     * num 商品数量
     * goods_id 商品id
     *
     *
     * Create by Peter
     * 2019/12/09 10:43:49
     * Email:904801074@qq.com
     * @return false|string
     */
    function addGoods(){


        $user_id=resolve('weapp_user')['id'];

        $is_find=\App\Model\ShopCar::where('goods_sku_id',request()->input('id'))
            ->where('user_id',$user_id)->first();


        if($is_find){

//            \App\Model\ShopCar::

            $is_find->fill([
                'num'=>$is_find->num+request()->input('num'),
                'status'=>1
            ])->save();


        }else{

            \App\Model\ShopCar::create([
                'goods_id'=>request()->input('goods_id'),
                'goods_sku_id'=>request()->input('id'),
                'num'=>request()->input('num',1),
                'status'=>1,
                'user_id'=>$user_id,
                'seller_id'=>-1
            ]);

        }


        return Json::code(1,'success',resolve('weapp_user'));
    }


    /**
     * @Weapp(type='no_check')
     * 购物车列表
     * Create by Peter
     * 2019/12/09 13:21:57
     * Email:904801074@qq.com
     * @return false|string
     */
    function getShopCarList(){

//        $user_id=resolve('weapp_user')['id'];

        $user_info=$this->getUserInfo(request()->header('token',null));

        if(!$user_info) return Json::code(2,'请先登录');


        $user_id=$user_info['id'];
//        $user_id=1;

        $list=\App\Model\ShopCar::with('goods')->with('goods_sku.goods_sku_detail')
            ->has('goods_sku')
            ->with('goods_sku.snapped_sku.snapped')
            ->orderBy('id','desc')
            ->where('user_id',$user_id)->get()??[];


        foreach ($list as $key=>$value){

            $list[$key]['sku_array']=$value['goods_sku']['goods_sku_detail'];

        }


        $list=$this->getGroupBySellerId($list);

        return Json::code(1,'success',$list);
    }


    function getGroupBySellerId($list){

        $temp=[];
        foreach ($list as $key=>$value){

            $temp[$value['seller_id']][]=$value;

        }

        return array_values($temp);

    }


    function changeStatus(){

        $status=request()->input('status');

        $id=request()->input('id');

        \App\Model\ShopCar::find($id)->fill(['status'=>$status])->save();


        return Json::code(1,'success');

    }

    function deleteGoods(){

        $user_id=resolve('weapp_user')['id'];

        $id=request()->input('id');

        \App\Model\ShopCar::where('id',$id)->where('user_id',$user_id)->delete();


        return Json::code(1,'success');

    }


    /**
     * 全部选中
     * Create by Peter
     * 2019/12/10 10:47:56
     * Email:904801074@qq.com
     * @return false|string
     */
    function selectAll(){

        $user_id=resolve('weapp_user')['id'];


        $list=\App\Model\ShopCar::where('user_id',$user_id)->get();


        foreach ($list as $key=>$value){


            $status=$value->status;

            if($status==2){

                $value->fill(['status'=>1])->save();

            }

        }

        return Json::code(1,'success');


    }


    /**
     * 全部取消选中
     * Create by Peter
     * 2019/12/10 10:47:42
     * Email:904801074@qq.com
     * @return false|string
     */
    function cancelAll(){

        $user_id=resolve('weapp_user')['id'];


        $list=\App\Model\ShopCar::where('user_id',$user_id)->get();


        foreach ($list as $key=>$value){


            $status=$value->status;

            if($status==1){

                $value->fill(['status'=>2])->save();

            }

        }

        return Json::code(1,'success');


    }

    /**
     * 修改数量
     * Create by Peter
     * 2019/12/10 10:47:27
     * Email:904801074@qq.com
     */
    function changeNum(){


        $user_id=resolve('weapp_user')['id'];

        $id=request()->input('id');

        $num=request()->input('num');

        $data=\App\Model\ShopCar::where('id',$id)
            ->where('user_id',$user_id)->first();

        $data->fill(['num'=>$num])->save();

        return Json::code(1,'success');

    }



}
